/**
 * JSparrow - Une version numérique du jeu "Attention Pirates !" - Copyright (C) 2014 Black Pearl
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. If
 * not, see <http://www.gnu.org/licenses/>.
 */
package App.Modele;

import java.awt.Point;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 * Classe de test de la classe Bateau.
 *
 * @author BlackPearl
 */
public class BateauTest {

    private Bateau bateau;
    private final static int idJoueur = 42;

    /**
     * Mettre en place le contexte des tests.
     */
    @Before
    public void setUp() {
        this.bateau = new Bateau(idJoueur);
    }

    /**
     * Test du constructeur de la classe Bateau.
     */
    @Test
    public void testBateau() {
        /**
         * @todo Modifier quand Bateau le sera
         */
        assertEquals(0, this.bateau.getNbTresors());
        assertEquals(1, this.bateau.getNbPirates());
        assertEquals(0, this.bateau.getNbCanons());
        assertEquals(idJoueur, this.bateau.getIdJoueur());
        assertEquals(new Point(0, 0), this.bateau.getEmplacement());
    }

    /**
     * Test de la méthode getDeplacementsPossibles de la classe Bateau.
     */
    @Test
    public void testGetDeplacementsPossibles() {
        int NB_RUNS = 100;
        for (int i = 0; i < NB_RUNS; i++) {
            int nbCanons = Configuration.random(0, 5);
            int nbPirates = 1;
            int nbTresors = Configuration.random(0, (nbCanons + nbPirates < 6 ? 0 : 1) * 6);
            this.bateau.setNbCanons(nbCanons);
            this.bateau.setNbPirates(nbPirates);
            this.bateau.setNbTresors(nbTresors);
            int n = (this.bateau.getNbPirates() == 0 ? 1 : this.bateau.getNbPirates());
            //int expected = (int) (6 * ((Math.pow(n, 2) + n) / 2));
            assertEquals(11, this.bateau.getDeplacementsPossibles().size());
        }
    }

    /**
     * Test de la méthode getDeplacementsPossibles de la classe Bateau.
     */
    @Test
    public void testReparer() {
        TypePiece[] listePieces = new TypePiece[6];
        listePieces[0] = TypePiece.Pirate;
        listePieces[1] = TypePiece.Pirate;
        listePieces[2] = TypePiece.Canon;
        listePieces[3] = TypePiece.Canon;
        listePieces[4] = TypePiece.Canon;
        listePieces[5] = TypePiece.Tresor;
        this.bateau.reparer(listePieces);
        assertArrayEquals(listePieces, this.bateau.getArrayPiece());
    }

    /**
     * Test de la méthode prendreTresor de la classe Bateau.
     */
    @Test
    public void testPrendreTresor() {
        TypePiece[] listePieces = new TypePiece[6];
        listePieces[5] = TypePiece.Pirate;
        this.bateau.reparer(listePieces);
        assertEquals(0, this.bateau.prendreTresor(1));
        listePieces[5] = TypePiece.Tresor;
        this.bateau.reparer(listePieces);
        assertEquals(0, this.bateau.prendreTresor(1));
        this.bateau.setNbTresors(0);
        assertEquals(4, this.bateau.prendreTresor(10));
    }

    /**
     * Test de la méthode deplacer de la classe Bateau.
     */
    @Test
    public void testDeplacer() {
        Point nouvelEmplacement = new Point(2, 2);
        this.bateau.deplacer(nouvelEmplacement);
        assertEquals(nouvelEmplacement, this.bateau.getEmplacement());
    }

    /**
     * Test de la méthode peutAttaquer de la classe Bateau.
     */
    @Test
    public void testPeutAttaquer() {
        this.bateau.setNbCanons(0);
        this.bateau.setNbPirates(0);
        assertEquals(false, this.bateau.peutAttaquer());
        this.bateau.setNbCanons(0);
        this.bateau.setNbPirates(1);
        assertEquals(false, this.bateau.peutAttaquer());
        this.bateau.setNbCanons(1);
        this.bateau.setNbPirates(1);
        assertEquals(true, this.bateau.peutAttaquer());
    }

    /**
     * Test de la méthode recoitAttaque de la classe Bateau.
     */
    @Test
    public void testRecoitAttaque() {
        TypePiece[] listePieces = new TypePiece[6];
        listePieces[0] = TypePiece.Tresor;
        this.bateau.reparer(listePieces);
        assertEquals(false, this.bateau.recoitAttaque(0));
        listePieces[0] = null;
        this.bateau.reparer(listePieces);
        assertEquals(false, this.bateau.recoitAttaque(0));
        listePieces[0] = TypePiece.Pirate;
        this.bateau.reparer(listePieces);
        assertEquals(true, this.bateau.recoitAttaque(0));
        listePieces[0] = TypePiece.Canon;
        this.bateau.reparer(listePieces);
        assertEquals(true, this.bateau.recoitAttaque(0));
    }

}
